package com.tom.sync;


import org.apache.rocketmq.client.producer.DefaultMQProducer;
import org.apache.rocketmq.client.producer.SendCallback;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;
import java.nio.charset.StandardCharsets;

public class Producer {
    public static void main(String[] args) throws Exception {
        // 创建生成者对象,指定组名
        DefaultMQProducer producer = new DefaultMQProducer("sync_hello_group");
        // 获取mq连接
        producer.setNamesrvAddr("127.0.0.1:9876");
        // 启动生产者
        producer.start();
        // 4.1.创建消息体
        // topic：消息主题 tags：此主题下的二级分类 body：消息的字节
        Message message = new Message("sync", "hello", "源码时代".getBytes(StandardCharsets.UTF_8));

        producer.send(message, new SendCallback() {
            @Override
            public void onSuccess(SendResult sendResult) {
                System.out.println("发送异步");
            }
            @Override
            public void onException(Throwable throwable) {
                throwable.printStackTrace();
                System.out.println("发送失败");
            }
        });

    }
}
